package com.example.manager.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.manager.entity.CourseSelection;
import com.example.manager.entity.CourseTeaching;
import com.example.manager.entity.Student;
import com.example.manager.repository.CourseSelectionRepository;
import com.example.manager.repository.CourseTeachingRepository;
import com.example.manager.repository.StudentRepository;

@Service
@Transactional
public class CourseSelectionServiceImpl implements CourseSelectionService {
	
	@Autowired
	private CourseSelectionRepository csRepository;
	@Autowired
	private StudentRepository stuRepo;
	@Autowired
	private CourseTeachingRepository ctRepo;
	
	@Override
	public List<CourseSelection> getByStudent(Student student) {
		
		return csRepository.getByStudent(student);
	}

	@Override
	public List<CourseSelection> findAll() {
		return csRepository.findAll();
	}

	@Override
	public List<CourseSelection> getByStudent(String sid) {
		Student stu = stuRepo.getOne(sid);
		return csRepository.getByStudent(stu);
		
	}

	@Override
	public List<CourseSelection> getByCourseTeachingId(Integer id) {
		CourseTeaching ct = ctRepo.getOne(id);
		
		return csRepository.getByCourseTeaching(ct);
	}

	@Override
	public String selectCourse(String sid, Integer courseTeachingId) {
		System.out.println(sid);
		System.out.println(courseTeachingId);
		Student stu = stuRepo.getOne(sid);
		CourseTeaching ct = ctRepo.getOne(courseTeachingId);
		
		List<CourseSelection> list = new ArrayList<CourseSelection>();
		list = csRepository.getByStudent(stu);
		for(int i=0;i<list.size();i++) {
			Integer ctId = list.get(i).getCourseTeaching().getId();
			if(ctId == courseTeachingId) {
				System.out.println("error"+"  "+i);
				return "exist";
			}
		}
		
		CourseSelection cs = new CourseSelection();
		cs.setStudent(stu);
		cs.setCourseTeaching(ct);
		csRepository.save(cs);
		return "success";
	}

	@Override
	public String cancelSelectCourse(Integer id) {
		if(csRepository.existsById(id)) {
			csRepository.deleteById(id);
			return "success";
		}
		
		return "fail";
	}

	@Override
	public String selectCourse(CourseSelection css) {
		String sid = css.getStudent().getSid();
		Integer courseTeachingId = css.getCourseTeaching().getId();
		
		System.out.println(sid);
		System.out.println(courseTeachingId);
		Student stu = stuRepo.getOne(sid);
		CourseTeaching ct = ctRepo.getOne(courseTeachingId);
		
		List<CourseSelection> list = new ArrayList<CourseSelection>();
		list = csRepository.getByStudent(stu);
		for(int i=0;i<list.size();i++) {
			Integer ctId = list.get(i).getCourseTeaching().getId();
			if(ctId == courseTeachingId) {
				System.out.println("error"+"  "+i);
				return "exist";
			}
		}
		CourseSelection cs = new CourseSelection();
		cs.setStudent(stu);
		cs.setCourseTeaching(ct);
		
		csRepository.save(cs);
		return "success";
		
	}

	@Override
	public CourseSelection getById(Integer id) {
		return csRepository.getOne(id);
	}

}
